//
//  ZQMyAdressPage.m
//  jianzhik
//
//  Created by huami on 2018/8/29.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "ZQAdressModels.h"
#import <MJExtension/MJExtension.h>
#import "ZQAddAdressPage.h"
#import "ZQMyAdressPage.h"
#import "ZQMyAdressTableViewCell.h"
#import "ZQMoneyModels.h"
@interface ZQMyAdressPage ()
/** listArr */
@property (nonatomic, strong) NSMutableArray <ZQAdressModels *> *listMuArr;
@end

@implementation ZQMyAdressPage
- (NSMutableArray<ZQAdressModels *> *)listMuArr
{
    if (!_listMuArr) {
        _listMuArr = [[NSMutableArray alloc] init];
    }
    return _listMuArr;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [ZQAFHelper afnetworkRequestWithGetURL:@"/v1/user/listcoin" parameters:parameters view:self.view finishedBlcok:^(NSDictionary *dict) {
        NSArray *arr= [ZQMoneyModels mj_objectArrayWithKeyValuesArray:dict[@"Data"]];
        
        if (arr.count == 0) {
            
                [UIHelper showTipsWithText:@"请使用超级管理员账号添加币种" inView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [ZQDataManger shareIntance].bTypeListArr = @[];
                    [self.navigationController popViewControllerAnimated:YES];
                    return ;
                });
            
        } else {
                NSMutableArray *marr = [NSMutableArray array];
                [arr enumerateObjectsUsingBlock:^(ZQMoneyModels *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [marr addObject:obj.Symbol];
                }];
                [ZQDataManger shareIntance].bTypeListArr = marr;
       
            
            [ZQAFHelper afnetworkRequestWithGetURL:@"/v1/user/listaddress" parameters:nil view:self.view finishedBlcok:^(NSDictionary *dict) {
                //        dict[Data]
                self.listMuArr = [ZQAdressModels mj_objectArrayWithKeyValuesArray:dict[@"Data"]];
                [UIHelper dismissLoadingInView:self.view];
                [self.tableView reloadData];
            }];
        }

    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.footButton setTitle:@"添加地址" forState:UIControlStateNormal];
    [self.footButton addTarget:self action:@selector(addAdress) forControlEvents:UIControlEventTouchUpInside];
    [self registerCell:[ZQMyAdressTableViewCell class]];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    RegisterNotify(@"deleteAddress", @selector(tabviewLoadData:))
}

- (void)tabviewLoadData:(NSNotification *)noti
{
    NSDictionary  *dic = [noti userInfo];
    
    ZQAdressModels *model = dic[@"model"];
    [self.listMuArr removeObject:model];
    
    [self.tableView reloadData];
    
}

- (void)dealloc
{
    RemoveNofify;
}

- (void)addAdress
{
    ZQAddAdressPage *vc = [ZQAddAdressPage new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listMuArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZQMyAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID  forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.model = self.listMuArr[indexPath.row];
    return cell;
}
@end
